package smartagdemo.tool;

import smartagdemo.bean.HumidityData;
import smartagdemo.bean.LightData;
import smartagdemo.bean.TemperatureData;
import smartagdemo.bean.TestData;

import java.util.List;

public class CalculateAVG {
    public static TestData CalculateTestAVG(List<TestData> testDataList){
        TestData testDataTemple=new TestData();
        double humidityAvg=0,temperatureAvg=0,lightAvg=0;
        double humiditySum=0,temperatureSum=0,lightSum=0;
        int count=0;
        for(TestData testData : testDataList){
            humiditySum+=testData.getHumidityData();
            temperatureSum+=testData.getTemperatureData();
            lightSum+=testData.getLightData();
            count++;
        }
        humidityAvg=humiditySum/count;
        temperatureAvg=temperatureSum/count;
        lightAvg=lightSum/count;
        testDataTemple.setHumidityData(humidityAvg);
        testDataTemple.setTemperatureData(temperatureAvg);
        testDataTemple.setLightData(lightAvg);

        return testDataTemple;
    }

    public static double CalculateHumidityAVG(List<HumidityData> humidityDataList){
        double avg=0;
        double sum=0;
        double count=0;
        for(HumidityData humidityData : humidityDataList){
            sum+=humidityData.getHumidity();
            count++;
        }
        avg=sum/(double) count;
        if(Double.isNaN(avg))avg=0;
        return avg;
    }
    public static double CalculateTemperatureAVG(List<TemperatureData> temperatureDataList){
        double avg=0;
        double sum=0;
        double count=0;
        for(TemperatureData temperatureData: temperatureDataList){
            sum+=temperatureData.getTemperature();
            count++;
        }
        avg=sum/(double)count;
        if(Double.isNaN(avg))avg=0;
        return avg;
    }
    public static double CalculateLightAVG(List<LightData> lightDataList){
        double avg=0;
        double sum=0;
        double count=0;
        for(LightData lightData : lightDataList){
            sum+=lightData.getLight();
            count++;
        }
        avg=sum/(double)count;
        if(Double.isNaN(avg))avg=0;
        return avg;
    }
}
